package top.onedream.buildsmallsoftware.cfg.Interceptor

import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Component
import org.springframework.web.servlet.HandlerInterceptor
import top.onedream.buildsmallsoftware.model.user.User
import top.onedream.buildsmallsoftware.util.TokenHolder

import top.onedream.buildsmallsoftware.util.TokenUtil
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse


@Component
class JwtInterceptor(
    private val tokenUtil: TokenUtil
) : HandlerInterceptor {

    private val logger: Logger = LoggerFactory.getLogger(JwtInterceptor::class.java)

    @Throws(Exception::class)
    override fun preHandle(
        request: HttpServletRequest,
        response: HttpServletResponse,
        handler: Any
    ): Boolean {
        try {
            val user: User? = tokenUtil.getLoginUser(request)
            logger.info("用户:" + user?.id + "开发访问")
        } catch (e: Exception) {
           TokenHolder.user
        }
        return true
    }


}